﻿package com.urbansquall.litmus
{
import com.urbansquall.utils.FPS;
import flash.display.*;
import flash.events.MouseEvent;
import flash.text.*;

public class TestManager extends Sprite
{
	private var m_testClasses : Array /* Class */;

	private var m_activeTestIndex : int = -1;
	private var m_activeTest : AbstractTest = null;
	
	private var m_name : TextField
	private var m_fps : FPS;
	
	public function TestManager() {}
	
	// Pass in an array of references to all the AbstractTest *classes*
	// We do this because classes are instantiated and disposed rather than simply start/stopped
	// This is to provide more direct clean up
	public function initialize( a_testClasses : Array /* Class */ ) : void
	{
		if( this.stage == null )
		{
			throw new Error( "TestManager can only be initialized after it has been added to the stage" );
		}
		m_testClasses = a_testClasses;
		m_activeTestIndex = -1;
		createUI();
		startNextTest();
	}
	
	private function createUI() : void
	{
		m_name = new TextField();
		m_name.selectable = false;
		m_name.mouseEnabled = false;
		m_name.defaultTextFormat = new TextFormat( "Verdana", 9, 0x000000 );
		m_name.backgroundColor = 0xFFFFFF;
		m_name.background = true;
		m_name.autoSize = TextFieldAutoSize.CENTER;
		m_name.text = "Loading test framework...";
		m_name.x = this.stage.stageWidth / 2 - m_name.width/2;
		addChild( m_name );
	
		createNextButton();
		createPreviousButton();
		createFPSCounter();
	}
	
	private function createFPSCounter() : void
	{
		m_fps = new FPS();
		m_fps.x = this.stage.stageWidth / 2 - m_fps.width / 2;
		m_fps.y = this.stage.stageHeight - m_fps.height;
		addChild( m_fps );
	}
	
	private function createNextButton() : void
	{
		var nextButtonLabel: TextField = new TextField();
		nextButtonLabel.selectable = false;
		nextButtonLabel.defaultTextFormat = new TextFormat( "Verdana", 9, 0xFFFFFF );
		nextButtonLabel.mouseEnabled = false;
		nextButtonLabel.autoSize = TextFieldAutoSize.LEFT;
		nextButtonLabel.text = "Next";
		
		var nextButtonContainer : Sprite = new Sprite();
		nextButtonContainer.buttonMode = true;
		nextButtonContainer.useHandCursor = true;
		nextButtonContainer.addEventListener( MouseEvent.CLICK, clickNext );
		nextButtonContainer.graphics.beginFill( 0x000000 );
		nextButtonContainer.graphics.drawRect( 0, 0, nextButtonLabel.width, nextButtonLabel.height );
		nextButtonContainer.x = this.stage.stageWidth - nextButtonLabel.width;
		addChild( nextButtonContainer );
		
		nextButtonContainer.addChild( nextButtonLabel );
	}
	
	private function createPreviousButton() : void
	{
		var previousButtonLabel: TextField = new TextField();
		previousButtonLabel.selectable = false;
		previousButtonLabel.defaultTextFormat = new TextFormat( "Verdana", 9, 0xFFFFFF );
		previousButtonLabel.mouseEnabled = false;
		previousButtonLabel.autoSize = TextFieldAutoSize.LEFT;
		previousButtonLabel.text = "Prev";
		
		var previousButtonContainer : Sprite = new Sprite();
		previousButtonContainer.buttonMode = true;
		previousButtonContainer.useHandCursor = true;
		previousButtonContainer.addEventListener( MouseEvent.CLICK, clickPrevious );
		previousButtonContainer.graphics.beginFill( 0x000000 );
		previousButtonContainer.graphics.drawRect( 0, 0, previousButtonLabel.width, previousButtonLabel.height );
		addChild( previousButtonContainer );
		
		previousButtonContainer.addChild( previousButtonLabel );
	}
	
	private function clickNext( a_event : MouseEvent ) : void
	{
		startNextTest();
	}
	
	private function clickPrevious( a_event : MouseEvent ) : void
	{
		startPreviousTest();
	}
	
	private function startNextTest() : void
	{
		m_activeTestIndex++;
		
		startTest();
	}
	
	private function startPreviousTest() : void
	{
		m_activeTestIndex--;
		
		startTest();
	}
	
	private function startTest() : void
	{
		disposeActiveTest();
		
		if( m_activeTestIndex < 0 )
		{
			m_activeTestIndex = m_testClasses.length - 1;
		}
		if( m_activeTestIndex >= m_testClasses.length )
		{
			m_activeTestIndex = 0;
		}
		
		createNewTest();
		
		m_fps.reset();
	}
	
	private function disposeActiveTest() : void
	{
		if( m_activeTest != null )
		{
			m_activeTest.dispose();
			removeChild( m_activeTest );
			m_activeTest = null;
		}
	}
	
	private function createNewTest() : void
	{
		var TestClass : Class = m_testClasses[ m_activeTestIndex ];
		m_activeTest = new TestClass();
		m_name.text = m_activeTest.title;
		addChildAt( m_activeTest, 0 );
	}
	
} // c
} // p